package com.example.chatroom.controller;

import com.example.chatroom.common.errorcode.ControllerErrorCodeConstants;
import com.example.chatroom.common.exception.ControllerException;
import com.example.chatroom.common.pojo.CommonResult;
import com.example.chatroom.controller.result.FriendListResult;
import com.example.chatroom.service.IFriendService;
import com.example.chatroom.service.dto.FriendListDTO;
import com.example.chatroom.service.dto.UserDTO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/friend")
@Slf4j
public class FriendController {
    @Autowired
    private IFriendService friendService;
    @GetMapping("/friendList")
    public CommonResult<List<FriendListResult>> getFriendList(HttpServletRequest request) {
        log.info("getFriendList : 获取好友列表");
        List<FriendListDTO> friendDTOS=friendService.getFriendList(request);
        return CommonResult.success(convertToFriendListResult(friendDTOS));
    }

    private List<FriendListResult> convertToFriendListResult(List<FriendListDTO> friendListDTOS) {
        if(friendListDTOS==null){
            log.error(ControllerErrorCodeConstants.USER_GET_FRIENDS_ERROR.getMsg());
            throw new ControllerException(ControllerErrorCodeConstants.USER_GET_FRIENDS_ERROR);
        }
        return friendListDTOS.stream().map(friendListDTO->{
            FriendListResult friendListResult=new FriendListResult();
            friendListResult.setFriendId(friendListDTO.getFriendId());
            friendListResult.setFriendName(friendListDTO.getFriendName());
            return friendListResult;
        }).collect(Collectors.toList());
    }
}
